import { createRouter, createWebHistory } from "vue-router";
import LocalCache from "@/utils/cache";
import { useUserInfo } from "@/stores/user";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/index.vue"),
    },
  ],
});
router.beforeEach(async (to, from) => {
  const user = useUserInfo();
  const token = LocalCache.getCache("token") || user.userInfos.accessToken;
  if (to.path === "/login" && !token) {
    return
  } else {
    if (!token) {
      return "/login";
    } else if (token && to.path === "/login") {
      return "/activity";
    }
  }
});

export default router;
